﻿using System;
using System.Data;
using System.Data.SqlClient;
using PointOfSale.Global;
using System.Windows.Forms;

namespace PointOfSale.Class
{
	public class clsStock
	{
		public clsStock()
		{
			
		}

		// Lay ve so luong hien co cua mot mat hang trong mot kho theo don vi nho nhat
		public static double SoLuongHangTrongKho(int itemId, int departmentId)
		{
			string str="select itemId, departmentId, unitId, qtyInHand "
				+ "from tblStock "
				+ "where itemId=" + itemId + " "
				+ "and departmentId=" + departmentId;
			DataTable dt1=clsDatabase.GetDataToTable(str, clsGlobal.g_DataConn);	
			if(dt1.Rows.Count == 0)
			{
				MessageBox.Show("Mặt hàng có itemId là " + itemId + " chưa có trong kho. ");
				return -1;
			}
			else
			{
				return Convert.ToDouble(dt1.Rows[0]["qtyInHand"]);
			}
		}

		public static DataTable GetAllItemForAnDepartment(int intDepartmentId)
		{
			string selectCommand = "select a.itemId, b.itemName "
				+ " from tblItemDepartment a inner join tblItem b "
				+ " on a.itemId = b.itemId where a.departmentId = " + intDepartmentId+ " order by ItemName" ;
			return clsDatabase.GetDataToTable(selectCommand, clsGlobal.g_DataConn);
		}

		public static DataTable GetAllItemByDepartmentAndServiceId(int departmentId, int serviceId)
		{
			string sc = " select a.itemId, b.itemName "
				+ " from tblItemDepartment a inner join tblItem b "
				+ " on a.itemId = b.itemId "
				+ " where a.departmentId = " + departmentId + " and b.serviceId = " + serviceId+  " order by ItemName" ;
			return clsDatabase.GetDataToTable(sc, clsGlobal.g_DataConn);
		}
		public static void UpdateQuantity(int intItemId, int intDepartmentId, double dbChange, string sign)
		{
			double dbOldQuantity = 0.0;
			string selectCommand = "select qtyInHand from tblStock where "
				+ " itemId = " + intItemId
				+ " and departmentId = " + intDepartmentId;
			DataTable dt1 = clsDatabase.GetDataToTable(selectCommand, clsGlobal.g_DataConn);
			if(dt1.Rows.Count == 0)
			{
				//MessageBox.Show("Trong bang tblStock hien chua co so luong mat hang nay");
			}
			else
			{
				dbOldQuantity = Convert.ToDouble(dt1.Rows[0][0]);
			}
			
			double dbNewQuantity = 0.0;
			if(sign == "-")
			{
				
				dbNewQuantity = dbOldQuantity - dbChange;
				
			}
			else
			{
				dbNewQuantity = dbOldQuantity + dbChange;
			}
			
			string updateCommand = "update tblStock set qtyInHand = " + dbNewQuantity.ToString().Replace(",", ".")
				+ " where itemId = " + intItemId
				+ " and departmentId = " + intDepartmentId;
			clsDatabase.ExecuteSQL(updateCommand, clsGlobal.g_DataConn);
		}

		public static void AddItemDepartment(int intItemId, int intDepartmentId, int intMinUnitId)
		{
			string insertCommand = "insert into tblStock (itemId, departmentId, unitId, qtyInHand) "
				+ " values(" + intItemId + ", " + intDepartmentId + ", " + intMinUnitId + ", 0)";
			clsDatabase.ExecuteSQL(insertCommand, clsGlobal.g_DataConn);
		}

		public static void CapNhatDonViChuan(int itemId, int departmentId, int unitId)
		{
			string insertCommand = "delete tblStock where itemId = " + itemId + " and departmentId ="+departmentId;
			clsDatabase.ExecuteSQL(insertCommand, clsGlobal.g_DataConn);
			 insertCommand = "insert into tblStock (itemId, departmentId, unitId, qtyInHand) "
				+ " values(" + itemId + ", " + departmentId + ", " + unitId + ", 0)";
			clsDatabase.ExecuteSQL(insertCommand, clsGlobal.g_DataConn);
			
		}
		public static int DaCoDongSoLuong(int itemId, int departmentId)
		{
			string sc="select * from tblStock where itemid=" + itemId + " and departmentId="+departmentId;
			DataTable dt=clsDatabase.GetDataToTable(sc, clsGlobal.g_DataConn);
			if(dt.Rows.Count==1)
			{
				return 1;
			}
			else
			{
				return 0;
			}
		}
		public static void InsertQuantity(int itemId, int departmentId, int unitId, double quantity)
		{
			string insertCommand="insert into tblStock(itemId, departmentId, unitId, qtyInHand) values (" + itemId + ", " + departmentId + ", " + unitId + ", " + quantity + ")";
			clsDatabase.ExecuteSQL(insertCommand, clsGlobal.g_DataConn);
		}
	}
}
